#include "phoneloginwidget.h"
#include "loginwidget.h"
#include "model/data.h"
#include "registerwidget.h"
#include "model/datacenter.h"
#include "toast.h"
#include "mainwidget.h"
#include <QGridLayout>

using namespace model;

PhoneLoginWidget::PhoneLoginWidget(QWidget *parent): QWidget{parent}
{
    // 1. 设置窗口的基本属性
    this->setFixedSize(420, 320);
    this->setWindowTitle("登录");
    this->setWindowIcon(QIcon(":/resource/image/logo.png"));
    this->setStyleSheet("QWidget { background-color: rgb(255, 255, 255); }");
    this->setAttribute(Qt::WA_DeleteOnClose);

    // 2. 创建核心布局管理器
    QGridLayout* layout = new QGridLayout();
    layout->setSpacing(10);
    layout->setContentsMargins(50, 15, 50, 10);
    this->setLayout(layout);

    // 3. 创建标题
    // 3. 创建标题区域（图标 + 文字）
    QWidget* titleWidget = new QWidget();
    titleWidget->setFixedHeight(50);
    QHBoxLayout* titleLayout = new QHBoxLayout(titleWidget);
    titleLayout->setContentsMargins(0, 0, 0, 5);

    // 创建图标 QLabel
    QLabel* iconLabel = new QLabel();
    QPixmap iconPixmap(":/resource/image/logo.png");
    iconPixmap = iconPixmap.scaled(42, 42, Qt::KeepAspectRatio, Qt::SmoothTransformation);
    iconLabel->setPixmap(iconPixmap);
    iconLabel->setFixedSize(42, 42);

    // 创建文字标题
    titleLabel = new QLabel();
    titleLabel->setText("登录");
    titleLabel->setAlignment(Qt::AlignVCenter | Qt::AlignLeft);
    titleLabel->setStyleSheet("QLabel { font-size: 32px; font-weight: 600; }");

    titleLayout->addStretch(1);
    titleLayout->addWidget(iconLabel);
    titleLayout->addSpacing(10); // 图标和文字之间的间距
    titleLayout->addWidget(titleLabel);
    titleLayout->addStretch(1);

    // 4. 创建手机号输入框
    phoneEdit = model::createLineEdit(QIcon(":/resource/image/phone.png"), "请输入手机号");

    // 5. 创建验证码输入框
    verifyCodeEdit = model::createLineEdit(QIcon(":/resource/image/mes_verifycode.png"), "请输入验证码");

    // 6. 创建发送验证码按钮
    sendVerifyCodeBtn = new QPushButton();
    sendVerifyCodeBtn->setFixedSize(100, 40);
    sendVerifyCodeBtn->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
    sendVerifyCodeBtn->setText("发送验证码");
    sendVerifyCodeBtn->setStyleSheet(R"(
        QPushButton { border: none; border-radius: 10px; background-color: transparent; }
        QPushButton:pressed { background-color: rgb(240, 240, 240); }
    )");

    // 7. 创建提交按钮
    submitBtn = new QPushButton();
    submitBtn->setFixedHeight(40);
    submitBtn->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed);
    submitBtn->setText("登录");
    QString btnGreenStyle = "";
    btnGreenStyle += "";
    submitBtn->setStyleSheet(R"(
        QPushButton { border: none; border-radius: 10px; background-color: rgb(44, 182, 61); color: white; }
        QPushButton:pressed { background-color: rgb(240, 240, 240); }
    )");

    // 8. 创建 "切换到用户名" 模式按钮
    QPushButton* userModeBtn = new QPushButton();
    userModeBtn->setFixedSize(100, 40);
    userModeBtn->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
    userModeBtn->setText("用户名登录");
    userModeBtn->setStyleSheet(sendVerifyCodeBtn->styleSheet());

    // 9. 切换注册模式
    switchModeBtn = new QPushButton();
    switchModeBtn->setFixedSize(100, 40);
    switchModeBtn->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
    switchModeBtn->setText("注册");
    switchModeBtn->setStyleSheet(sendVerifyCodeBtn->styleSheet());

    // 10. 添加到布局管理器
    layout->addWidget(titleWidget, 0, 0, 1, 5);
    layout->addWidget(phoneEdit, 1, 0, 1, 5);
    layout->addWidget(verifyCodeEdit, 2, 0, 1, 4);
    layout->addWidget(sendVerifyCodeBtn, 2, 4, 1, 1);
    layout->addWidget(submitBtn, 3, 0, 1, 5);
    layout->addWidget(userModeBtn, 4, 0, 1, 1);
    layout->addWidget(switchModeBtn, 4, 4, 1, 1);

    // 11. 添加信号槽
    connect(switchModeBtn, &QPushButton::clicked, this, &PhoneLoginWidget::switchMode);

    connect(userModeBtn, &QPushButton::clicked, this, [=]() {
        LoginWidget* loginWidget = new LoginWidget(nullptr);
        loginWidget->show();
        this->close();
    });

    connect(sendVerifyCodeBtn, &QPushButton::clicked, this, &PhoneLoginWidget::sendVerifyCode);
    // 倒计时
    timer = new QTimer(this);
    connect(timer, &QTimer::timeout, this, &PhoneLoginWidget::countDown);

    connect(submitBtn, &QPushButton::clicked, this, &PhoneLoginWidget::clickSubmitBtn);
}

void PhoneLoginWidget::switchMode()
{
    if (isLoginMode) {
        // 切换到注册模式
        this->setWindowTitle("注册");
        titleLabel->setText("注册");
        submitBtn->setText("注册");
        switchModeBtn->setText("登录");
    } else {
        // 切换到登录模式
        this->setWindowTitle("登录");
        titleLabel->setText("登录");
        submitBtn->setText("登录");
        switchModeBtn->setText("注册");
    }
    isLoginMode = !isLoginMode;
}

void PhoneLoginWidget::countDown(){
    if(leftTime <= 1){
        // 时间到, 发送按钮设为可用状态, 并且停止定时器
        sendVerifyCodeBtn->setEnabled(true);
        sendVerifyCodeBtn->setText("发送验证码");
        timer->stop();
        leftTime = 30;
        return;
    }

    leftTime -= 1;
    sendVerifyCodeBtn->setText(QString::number(leftTime) + " s");
    if (sendVerifyCodeBtn->isEnabled()) {
        sendVerifyCodeBtn->setEnabled(false);
    }
}

void PhoneLoginWidget::sendVerifyCode(){
    // 1. 获取手机号
    const QString& phone = phoneEdit->text();
    if(phone.isEmpty()){
        Toast::showMessage("手机号为空, 请输入手机号");
        return;
    }
    this->currentPhone = phone; // 保存当前手机号
    // 2. 发送网络请求 获取验证码
    DataCenter* dataCenter = DataCenter::getInstance();
    connect(dataCenter, &DataCenter::getVerifyCodeDone, this, &PhoneLoginWidget::sendVerifyCodeDone, Qt::UniqueConnection);
    dataCenter->getVerifyCodeAsync(phone);

    // 3. 开启定时器倒计时
    timer->start(1000); // 每隔1s触发 countDown
}

void PhoneLoginWidget::sendVerifyCodeDone(){
    Toast::showMessage("验证码已发送");
}

void PhoneLoginWidget::clickSubmitBtn(){
    // 1. 从输入框中获取内容
    // 注意: 此处不要从输入框直接拿电话. 可能用户故意搞事情, 输入验证码的时候, 输入的是电话1, 点击提交的时候, 输入的是电话2;
    const QString& phone = this->currentPhone;
    const QString& verifyCode = verifyCodeEdit->text();
    if (phone.isEmpty() || verifyCode.isEmpty()) {
        Toast::showMessage("电话或者验证码不应该为空");
        return;
    }

    // 2. 发送请求
    DataCenter* dataCenter = DataCenter::getInstance();
    if (isLoginMode) {
        // 登录
        connect(dataCenter, &DataCenter::phoneLoginDone, this, &PhoneLoginWidget::phoneLoginDone, Qt::UniqueConnection);
        dataCenter->phoneLoginAsync(phone, verifyCode);
    } else {
        // 注册
        connect(dataCenter, &DataCenter::phoneRegisterDone, this, &PhoneLoginWidget::phoneRegisterDone, Qt::UniqueConnection);
        dataCenter->phoneRegisterAsync(phone, verifyCode);
    }
}

void PhoneLoginWidget::phoneLoginDone(bool ok, const QString &reason){
    if (!ok) {
        Toast::showMessage("登录失败! " + reason);
        return;
    }
    // 跳转到主窗口
    MainWidget* mainWidget = MainWidget::getInstance();
    mainWidget->show();
    // 关闭自己
    this->close();
}


void PhoneLoginWidget::phoneRegisterDone(bool ok, const QString &reason){
    // 是否注册成功
    if (!ok) {
        Toast::showMessage("注册失败! " + reason);
        return;
    }
    Toast::showMessage("注册成功!");

    switchMode(); // 跳转到登录界面
    verifyCodeEdit->clear(); // 清空输入框
    leftTime = 1; // 处理倒计时的按钮
}


























